/***
 * @Author: 玄绮 shijiahansi@qq.com
 * @Date: 2024-05-22 19:40:57
 * @LastEditors: 玄绮 shijiahansi@qq.com
 * @LastEditTime: 2024-05-24 13:15:47
 * @FilePath: \client\src\app\client\Client.cpp
 * @Description:
 * @
 * @Copyright (c) 2024 by 玄绮, All Rights Reserved.
 */
#include <Client.h>
#include <QDebug>
#include <QHostAddress>
#include <qdebug.h>
#include <qobjectdefs.h>

extern QString TCP_SERVER_ADDR;
extern int     TCP_SERVER_PORT;

Client::Client() {
    socket = new QTcpSocket(this);
    connect(socket, &QTcpSocket::connected, this, &Client::signalConnected);
    connect(socket, &QTcpSocket::disconnected, this, &Client::signalDiconnected);
    connect(socket, &QTcpSocket::readyRead, this, &Client::onReadyRead);
    // connect(socket, &QTcpSocket::, this, &Client::onReadyRead);
}

Client::~Client() {}

void Client::connectToHost(QString address, int port) {
    socket->connectToHost(QHostAddress(address), port, QIODevice::ReadWrite);
}

std::vector<std::shared_ptr<Client::FriendInfo>> Client::firends() {
    return m_firends;
}

std::vector<std::shared_ptr<Client::ChatRoomInfo>> Client::rooms() {
    return m_rooms;
}

void Client::onReadyRead() {
    qDebug() << "onTcpReadyRead";

    while (socket->bytesAvailable() > ::proto::packge::Packge::HeaderLength) {
        auto pack = std::make_shared<::proto::packge::Packge>();
        int  len  = 0;
        try {
            len          = socket->read((char*)&pack->header, pack->HeaderLength);
            auto& header = pack->header;
            if (
                header.body_length <= 0 ||
                header.version != proto::packge::VERSION10 ||
                header.proto_type != proto::packge::ProtoBuf) {
                std::cerr << "error proto: " << std::endl;
                return;
            }
            pack->data.resize(header.body_length);
            socket->read(pack->data.data(), header.body_length);
            pack_handler(pack);
        }
        catch (const std::exception e) {
            qDebug() << e.what();
        }
    }
}

void Client::send_packge(::proto::packge::Packge::shared_ptr pack) {
    std::string ss;
    pack->SerializeToString(ss);
    socket->write(QByteArray::fromStdString(ss));
}

void Client::onMessage(std::shared_ptr<proto::data::Message> msg) {
    emit signalMessage(msg);
}

void Client::onRegister(int code, String message, std::shared_ptr<UserInfo> user) {
    // emit signalRegister()
    emit signalRegister(code, QString::fromStdString(message), user);
};

void Client::onLogin(int code, String message, std::shared_ptr<UserInfo> user) {
    myself = *user;
    GetFriendList();
    GetRoomList();
    emit signalLogin(code, QString::fromStdString(message));
};

void Client::onGetUserInfo(int code, String message, std::shared_ptr<UserInfo> user) {

};

void Client::onChangUserInfo(int code, String message, std::shared_ptr<UserInfo> user) {};

void Client::onSearchAuthor(int code, String message, std::vector<std::shared_ptr<UserInfo>> users) {
    emit signalSearchAuthor(users);
};

void Client::onSearchGroup(int code, String message, std::vector<std::shared_ptr<ChatRoomInfo>> rooms) {
    emit signalSearchGroup(rooms);
};

void Client::onAddFriends(int code, String message, std::shared_ptr<FriendInfo> info) {
    if (!code)
        m_firends.push_back(info);
    emit signalAddFriends(code, QString::fromStdString(message), info);
};

void Client::onDelFriends(int code, String message, String firendsId) {};

void Client::onCreateGroup(int code, String message, std::shared_ptr<ChatRoomInfo> room) {};

void Client::onCreateRoom(int code, String message, std::shared_ptr<ChatRoomInfo> room) {};

void Client::onJoinGroup(int code, String message, std::shared_ptr<ChatRoomInfo> room) {};

void Client::onQuitGroup(int code, String message, String roomId) {};

void Client::onSetAccount(int code, String message) {};

void Client::onSetUserInfo(int code, String message, std::shared_ptr<UserInfo> user) {
    myself = *user;
    emit signalSetUserInfo(code, QString::fromStdString(message));
};

void Client::onGetFriendList(int code, String message, std::vector<std::shared_ptr<FriendInfo>> users) {
    if (!code) {
        m_firends = users;
    }
    emit signalGetFriendList(code, QString::fromStdString(message));
};

void Client::onGetRoomList(int code, String message, std::vector<std::shared_ptr<ChatRoomInfo>> rooms) {
    if (!code) {
        m_rooms = rooms;
    }
    emit signalGetRoomList(code, QString::fromStdString(message));
}

void Client::onGetRoomMembers(int code, String message, String roomId, std::vector<std::shared_ptr<UserInfo>>) {}

void Client::onVerifyVerificationCode(int code, String message) {
    emit signalVerifyVerificationCode(code, QString::fromStdString(message));
}
void Client::onSendVerificationCode(int code, String message, String timeout) {
    emit signalSendVerificationCode(code, QString::fromStdString(message), QString::fromStdString(timeout));
};
